package com.jd.order.consumer;

import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.mapper.OrderPetAcquisitionMapper;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.nio.charset.StandardCharsets;


@Controller
// 监听器
@RocketMQMessageListener(
        consumerGroup = "service-kill-consumer",
        topic = "petOrder",
        selectorExpression = "*",
        messageModel = MessageModel.CLUSTERING
)


public class orderConsumer implements RocketMQListener<Message> {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;


    @Override
    public void onMessage(Message message) {
        // 判断消息
        if (message.getBody() == null || message.getBody().length == 0){
            return;
        }

        // 将消息转化为string对象
        String st = new String(message.getBody(), StandardCharsets.UTF_8);
        // 再转换为long
        Long aLong = Long.valueOf(st);

        // 查询订单
        OrderPetAcquisition loadById = orderPetAcquisitionMapper.loadById(aLong);
        if (loadById.getState() == 1){
            orderPetAcquisitionMapper.remove(aLong);
        }

    }
}
